#include "PlSDLSimpleNet.h"
#include "simpletcplua.h"
#include "../core/LuaUtils.h"


void SimpleTcpLua::pushStaticFuncs(lua_State* L)
{
	static const struct luaL_Reg mylib [] = {
		{"ListenForConnections", li_listenForConnections},
		{"Connect", li_connect},
		{"Disconnect", li_disconnect},
		{"SendMessage", li_sendMessage},
		{"AddMessageHandler", li_addMessageHandler},
		{"RemoveMessageHandler", li_removeMessageHandler},
		{"AddConnectionHandler", li_addConnectionHandler},
		{"RemoveConnectionHandler", li_removeConnectionHandler},
		{"RenameConnection", li_renameConnection},
		{"GetConnectionName", li_getConnectionName},
		{NULL, NULL}  /* sentinel */
	};

	// dump them into said table
	luaL_register(L, NULL, mylib);
}

int SimpleTcpLua::li_listenForConnections(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	op->listenForConnections(a1);
	
	return 0;
}


int SimpleTcpLua::li_connect(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = lua_tostring(L, 2);
	int a2 = (int)lua_tointeger(L, 3);
	op->connect(a1, a2);
	
	return 0;
}


int SimpleTcpLua::li_disconnect(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	op->disconnect(a1);
	
	return 0;
}


int SimpleTcpLua::li_sendMessage(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	std::string a2 = lua_tostring(L, 3);
	IData a3 = popIData(L, 4);
	op->sendMessage(a1, a2, a3);
	
	return 0;
}


int SimpleTcpLua::li_addMessageHandler(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = lua_tostring(L, 2);
	AnyAOPtr a2 = unboxAOPtr(L, 3);
	op->addMessageHandler(a1, a2);
	
	return 0;
}


int SimpleTcpLua::li_removeMessageHandler(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	AnyAOPtr a1 = unboxAOPtr(L, 2);
	op->removeMessageHandler(a1);
	
	return 0;
}


int SimpleTcpLua::li_addConnectionHandler(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	std::string a1 = lua_tostring(L, 2);
	AnyAOPtr a2 = unboxAOPtr(L, 3);
	op->addConnectionHandler(a1, a2);
	
	return 0;
}


int SimpleTcpLua::li_removeConnectionHandler(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	AnyAOPtr a1 = unboxAOPtr(L, 2);
	op->removeConnectionHandler(a1);
	
	return 0;
}


int SimpleTcpLua::li_renameConnection(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	std::string a2 = lua_tostring(L, 3);
	op->renameConnection(a1, a2);
	
	return 0;
}


int SimpleTcpLua::li_getConnectionName(lua_State* L)
{
	SimpleTCPConnection* op = (SimpleTCPConnection*)typeCheckUnboxAO(L,1,"SimpleTCPConnection");
	if(op == NULL){
		LOG(logERROR) << "Null self!";
	return 0;}
	int a1 = (int)lua_tointeger(L, 2);
	std::string ret = op->getConnectionName(a1);
	lua_pushlstring(L, ret.data(), ret.length());
	return 1;
}


